define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/contacts/index/',
                    add_url: 'facrm/customer/contacts/add'
                }
            });

            //联系人
            var contact  = $("#table");
            //在普通搜索渲染后
            contact.on('post-common-search.bs.table', function (event, table) {
      
            });
            contact.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar',
                queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                    //params.filter='{"customer_id":'+customer_id+'}';//只是查当前客户的联系人
                    return params
                },
                columns: [
                    [
                        {
                            field: 'scene_id',
                            title: __('联系人场景'),
                            visible: false,
                            formatter: function (v,r) {
                                return scene_list[v];
                            },searchList: scene_list},
                        {field: 'customer.name', title: __('客户名称'),operate:false,search:false},
                        {field: 'customer_id', title: __('客户'), visible:false,addclass: 'selectpage',
							extend: 'data-source="facrm/customer/index?filter=%7B\"scene_id\"%3A\"1\"%7D" data-field="name" data-orderBy="id desc"'},
                        {field: 'name', title: __('姓名')},
                        {field: 'mobile', title: __('手机')},
                        {field: 'telephone', title: __('电话')},
                        {field: 'email', title: __('邮箱')},
                        {field: 'wechat', title: __('微信')},
                        {field: 'post', title: __('职务')},
                        {field: 'sex', title: __('性别')},
                        {field: 'decision', title: __('决策人')},
                        {field: 'detail_address', title: __('地址')},
                        {field: 'remark', title: __('备注')},
                        {field: 'next_time', title: __('下次联系'),formatter:Table.api.formatter.datetime, operate:'RANGE',addclass:'datetimerange' ,sortable: true},
                        {field: 'create_time', title: __('创建时间'),formatter:Table.api.formatter.datetime, operate:'RANGE',addclass:'datetimerange' ,sortable: true},
                        {field: 'operate', title: __('Operate'), table: contact,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: '',
                                    title: '修改联系人',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url: 'facrm/customer/contacts/edit',

                                },
                                {name: 'del',
                                    text: '',
                                    title: '删除',
                                    confirm:"确认删除联系人？",
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger  btn-ajax',
                                    url: 'facrm/customer/contacts/del',
                                    success:function(data,ret){
                                        $("a.btn-refresh").trigger("click");
                                    },
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }

                    ]
                ],
                searchFormVisible:true, //打开搜索
            });
            // 初始化表格
            contact.bootstrapTable();
            Table.api.bindevent(contact);//当内容渲染完成后
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name="+field+"]").val(value);
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();

        },
        correlation: function () {

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/business/contacts/correlation/customer_id/'+customer_id,
                }
            });
            var checkRow = [];
            var multiple = Backend.api.query('multiple');
            multiple = multiple == 'true' ? true : false;
            var table = $("#table");
            table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function (e, row) {
                if (e.type == 'check' || e.type == 'uncheck') {
                    row = [row];
                } else {
                    checkRow = [];
                }
                $.each(row, function (i, j) {
                    if (e.type.indexOf("uncheck") > -1) {
                        var index = checkRow.indexOf(j);
                        if (index > -1) {
                            checkRow.splice(index, 1);
                        }
                    } else {
                        checkRow.indexOf(j) == -1 && checkRow.push(j);
                    }
                });
            });

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $(".form-commonsearch select[name=scene_id]").val(4);
 
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                showToggle: false,
                showExport: false,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'state', checkbox: true, visible: true, operate: false},
                        {field: 'scene_id',title: __('场景'),visible: false, searchList: {4:"全部"}},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('姓名')},
                        {field: 'mobile', title: __('手机')},
                        {field: 'telephone', title: __('电话')},
                        {field: 'email', title: __('邮箱')},
                        {field: 'wechat', title: __('微信')},
                        {field: 'remark', title: __('备注')},
                        {
                            field: 'operate', title: __('Operate'), events: {
                                'click .btn-chooseone': function (e, value, row, index) {
                                    Fast.api.close([row]);
                                },
                            }, formatter: function () {
                                return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                            }
                        }
                    ]
                ]
            });

            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                Fast.api.close(checkRow);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            require(['upload'], function (Upload) {
                Upload.api.upload($("#toolbar .faupload"), function () {
                    $(".btn-refresh").trigger("click");
                });
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});